<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" v-if="optType !='view'">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:md:bom:add']"
        >新增
        </el-button>

      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:md:bom:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="产品编码" align="center" prop="goods_code"/>
      <el-table-column label="批次号" align="center" prop="batch_code"/>
      <el-table-column label="单位" align="center" prop="measure_unit"/>
      <el-table-column label="数量" width="90px" align="center" prop="quantity">
      </el-table-column>
      <el-table-column label="库位" width="90px" align="center" prop="area_name"/>
      <el-table-column label="有效期" width="90px" align="center" prop="expire_date"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="optType !='view'">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:md:bom:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:md:bom:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品BOM关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="产品物料" prop="goods_code">
          <el-input v-model="form.goods_code" placeholder="请选择产品物料" disabled>
            <el-button slot="append" @click="handleGoodsSelect('bd_goods')" icon="el-icon-search" v-if="v_if"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="批次号" prop="batch_code" v-if="v_if">
          <el-input v-model="form.batch_code" placeholder="请输入批次号"/>
        </el-form-item>
        <el-form-item label="单位" prop="measure_unit">
          <el-select v-model="form.measure_unit" placeholder="请选择单位">
            <el-option v-for="dict in dict.type.bd_goods_measure_unit" :key="dict.value" :label="dict.label"
                       :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库数量" prop="quantity">
          <el-input-number v-model="form.quantity" :min="1" label="入库数量"></el-input-number>
        </el-form-item>
        <el-form-item label="库位" prop="area_id">
          <el-select v-model="form.area_id" clearable placeholder="请选择">
            <el-option
              v-for="item in areaNames"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="单据有效期" prop="expire_date">
          <el-date-picker clearable
                          v-model="form.expire_date"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="删除扩展属性"
      :visible.sync="isFlag"
      width="30%">
      <span>是否确认删除当前所选商品扩展属性的数据项？</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="isFlag = false">取 消</el-button>
    <el-button type="primary" @click="isFlagMethod">确 定</el-button>
  </span>
    </el-dialog>
    <dictSelect ref="dictSelect" @onDictSelectd="onDictSelectd"/>
  </div>
</template>

<script>
import {getGoodsLine, getGoodsValue, UpdateGoodsLine, InsertGoodsLine, delGoodsLine} from "@/api/zxbd/goods"
import {
  deleteLineOne,
  DeleteOrderLine,
  getAreaWarehouse, getLineInfo,
  getOrderLine, PostOrderLine, PutOrderLine
} from "@/api/wms/order";

import dictSelect from "../../../components/dictSelect.vue"

export default {
  name: "Bom",
  dicts: ["sys_yes_no", "bd_goods_measure_unit"],
  components: {dictSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      codes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      areaNames: {},
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      v_if:false,
      flag: false,
      // 产品BOM关系表格数据
      bomList: [],
      deleteList: {},
      goodsValues: {},
      goodsEcho: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        parent_code: this.$route.query && this.$route.query.parent_code,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        batch_code: [
          {required: true, message: "批次号不能为空", trigger: "blur"}
        ],
        goods_code: [
          {required: true, message: "产品物料不能为空", trigger: "blur"}
        ],
        area_id: [
          {required: true, message: "库位不能为空", trigger: "blur"}
        ]
      },
      isFlag: false
    };
  },
  props: {
    optType: undefined,
    parent_code: undefined,
    name: undefined
  },
  created() {
    this.getList();
    this.getGoodsValue();

  },
  methods: {
    getAreaWarehouse() {
      getAreaWarehouse().then(res => {
        this.areaNames = res.data
      })
    },
    //字典选择后
    onDictSelectd(param) {
      if (param != undefined && param != null) {
        this.form.goods_code = param[0].dict_value;
      }
    },
    handleGoodsSelect(param) {
      console.log(param)
      var dictParams = {
        dict_type: param
      }
      this.$refs.dictSelect.show(dictParams)
    },
    getGoodsValue() {
      getGoodsValue().then(res => {
        this.goodsValues = res.data
      })
    },
    /** 查询产品BOM关系列表 */
    getList() {
      this.getAreaWarehouse()
      this.loading = true;
      console.log(this.queryParams.parent_code,"======<<<<")
      getOrderLine(this.queryParams.parent_code).then(response => {
        this.bomList = response.data;
        console.log(this.bomList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goods_code: null,
        parent_code: this.queryParams.parent_code,
        batch_code: undefined,
        expire_date: null,
        area_id: null,
        remark: null,
        quantity: 1,
        measure_unit: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.deleteList = selection
      this.codes = selection.map(item => item.code)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.v_if = true
      this.flag = true
      console.log(">>>>>>>>>>>>", this.form)
      this.open = true
      this.title = "添加商品扩展属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.v_if = false
      this.flag = false
      const data = row
      getLineInfo(data).then(response => {
        this.form = response.data;
        this.goodsEcho = response.data.attr_value
        this.open = true;
        this.title = "修改商品扩展属性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.flag) {
        this.submitAddForm()
      } else {
        this.$refs["form"].validate(valid => {
          if (valid) {
            PutOrderLine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        });
      }
    },

    /** 提交按钮 */
    submitAddForm() {
      PostOrderLine(this.form).then(response => {
        this.$modal.msgSuccess("添加成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.isFlag = true
      var b = (JSON.stringify(this.deleteList) == "{}");
      if (b) {
        this.deleteList = row
      }
    },
    isFlagMethod() {
      if (this.deleteList[1] === undefined) {
        deleteLineOne(this.deleteList).then(() => {
          this.getList();
          this.deleteList = {}
          this.isFlag = false
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      } else {
        DeleteOrderLine(this.deleteList).then(() => {
          this.getList();
          this.deleteList = {}
          this.isFlag = false
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      }
    }
  }
};
</script>
